package com.stockz.equity.charting.painter.axis;

import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import com.stockz.equity.charting.util.Constant;
import com.stockz.equity.charting.util.MathUtil;
import com.stockz.equity.charting.view.StockChartPanelSkin;
import com.stockz.equity.charting.view.StockChartPanelState;
import com.stockz.equity.charting.view.StockChart;
import com.stockz.equity.pattern.Painter;

@Component
@Lazy
public class PriceAxisPainter implements Painter<StockChart>{

	private int[] currentYmarks;
	
	@Override
	public void paint(Graphics2D g,StockChart view, Rectangle rectangle) {
		StockChartPanelSkin skin = view.getStockChartPanel().getStockChartPanelSkin();
		StockChartPanelState state = view.getStockChartPanel().getStockChartPanelState();
		
		g.setColor(skin.getAxisPriceColor());
		
		Rectangle r = state.getDrawingRectangle();
		g.drawLine(r.x + r.width, r.y, r.x + r.width, r.y+r.height);
		
		double min = state.getMinimumPriceVisible();
		double max = state.getMaximumPriceVisible();
		int div = skin.getAxisDevider();
		double increment = (max - min)/div;
		if(min == 0 || max == 0 || increment == 0){
			return;
		}
		
		/*  */
		double mult = Math.pow(10,Math.floor(Math.log10(increment)));
		double v[] = new double[]{1,2,2.5,5,10};
		
		for(int d = 1; d < v.length; d++){
			if(increment >= v[d-1]*mult && increment < v[d]*mult){
				increment = v[d-1]*mult;
				break;
			}
		}
		
		mult = Math.pow(10,Math.floor(Math.log10(min)));
		for(double i = 0; i < 10; i = i + 0.25){
			if(min >= i*mult && min < (i+0.25)*mult){
				min = i*mult;
				break;
			}
		}
		
		/*  */
		
		List<Integer> ymarks = new ArrayList<Integer>();
		
		
		int lastTextY = Integer.MAX_VALUE;
		int height = (int)g.getFont().getStringBounds("M", g.getFontRenderContext()).getHeight();
		
		double printedPrice = min;
		int i = 1;
		while(printedPrice <= max){
			
			double val = min+i*increment;
			if(val > max){
				val = max;
			}
			
			g.setColor(skin.getAxisVolumeColor());
			int Y = MathUtil.fitY(rectangle.y, 
					rectangle.y + rectangle.height, 
					state.getMinimumPriceVisible(), state.getMaximumPriceVisible(), 
					val,
					state.isSemiLog());
			
			
			if(Y < lastTextY - height -3 && Y >= rectangle.y){
				g.drawLine(rectangle.x + rectangle.width - skin.getShortTickLength(), 
						Y,
						rectangle.x + rectangle.width, 
						Y);
				g.setColor(skin.getTextColor());
				g.setFont(skin.getFont());
				if(Y > rectangle.y && Y < rectangle.y + rectangle.height){
					ymarks.add(Y);
				}
				
				g.drawString(Constant.priceNumberFormat.format(val), 
						rectangle.x + rectangle.width - (int)g.getFont().getStringBounds(Constant.priceNumberFormat.format(val), g.getFontRenderContext()).getWidth(), 
						Y+height+3);
				lastTextY = Y;
			}
			if(val == max){
				printedPrice = val+1;
			}else{
				printedPrice = val;
			}
			
			i++;
		}
		
		
		currentYmarks = new int[ymarks.size()];
		for(i = 0; i < ymarks.size(); i++){
			currentYmarks[i] = ymarks.get(i);
		}		
	}

	public int[] getCurrentYmarks() {
		return currentYmarks;
	}
	
}